/**
 * 表单配置生成器
 */

const validateTime = (rule, value, callback) => {
  if (!value || value.length !== 2) {
    callback(new Error('请选择时间'))
  } else {
    // 验证是否是同一天
    const startDate = new Date(value[0])
    const endDate = new Date(value[1])

    const isSameDay =
      startDate.getFullYear() === endDate.getFullYear() &&
      startDate.getMonth() === endDate.getMonth() &&
      startDate.getDate() === endDate.getDate()

    if (!isSameDay) {
      callback(new Error('时间不能跨天'))
    } else if (startDate.getTime() >= endDate.getTime()) {
      callback(new Error('结束时间必须大于开始时间'))
    } else {
      callback()
    }
  }
}

/**
 * 创建输入框配置
 */
export const createInput = (label, prop, options = {}) => ({
  type: 'input',
  label,
  prop,
  placeholder: options.placeholder,
  maxlength: options.maxlength,
  showPassword: options.showPassword,
  inputType: options.inputType,
  span: options.span,
  rules: options.rules,
  props: options.props,
  ...options,
})

/**
 * 创建数字输入框配置
 */
export const createNumber = (label, prop, options = {}) => ({
  type: 'number',
  label,
  prop,
  min: options.min,
  max: options.max,
  step: options.step,
  span: options.span,
  rules: options.rules,
  props: options.props,
  ...options,
})

/**
 * 创建选择器配置
 */
export const createSelect = (label, prop, options = {}) => ({
  type: 'select',
  label,
  prop,
  options: options.options,
  dictKey: options.dictKey,
  multiple: options.multiple,
  clearable: options.clearable,
  optionProps: options.optionProps,
  span: options.span,
  rules: options.rules,
  props: options.props,
  ...options,
})

/**
 * 创建单选框组配置
 */
export const createRadio = (label, prop, options = {}) => ({
  type: 'radio',
  label,
  prop,
  options: options.options,
  dictKey: options.dictKey,
  optionProps: options.optionProps,
  span: options.span,
  rules: options.rules,
  props: options.props,
  ...options,
})

/**
 * 创建复选框组配置
 */
export const createCheckbox = (label, prop, options = {}) => ({
  type: 'checkbox',
  label,
  prop,
  options: options.options,
  dictKey: options.dictKey,
  optionProps: options.optionProps,
  span: options.span,
  rules: options.rules,
  props: options.props,
  ...options,
})

/**
 * 创建日期选择器配置
 */
export const createDate = (label, prop, options = {}) => ({
  type: 'date',
  label,
  prop,
  dateType: options.dateType || 'date',
  valueFormat: options.valueFormat || 'YYYY-MM-DD',
  startPlaceholder: options.startPlaceholder,
  endPlaceholder: options.endPlaceholder,
  rangeSeparator: options.rangeSeparator,
  span: options.span,
  rules: options.rules,
  props: options.props,
  ...options,
})

/**
 * 创建时间选择器配置
 */
export const createTime = (label, prop, options = {}) => ({
  type: 'time',
  label,
  prop,
  valueFormat: options.valueFormat || 'HH:mm:ss',
  span: options.span,
  rules: options.rules,
  props: options.props,
  ...options,
})

/**
 * 创建开关配置
 */
export const createSwitch = (label, prop, options = {}) => ({
  type: 'switch',
  label,
  prop,
  activeValue: options.activeValue,
  inactiveValue: options.inactiveValue,
  activeText: options.activeText,
  inactiveText: options.inactiveText,
  span: options.span,
  rules: options.rules,
  props: options.props,
  ...options,
})

/**
 * 创建文本域配置
 */
export const createTextarea = (label, prop, options = {}) => ({
  type: 'textarea',
  label,
  prop,
  rows: options.rows || 4,
  maxlength: options.maxlength,
  span: options.span || 24,
  rules: options.rules,
  props: options.props,
  ...options,
})

/**
 * 创建树形选择器配置
 */
export const createTreeSelect = (label, prop, options = {}) => ({
  type: 'tree-select',
  label,
  prop,
  options: options.options || [],
  treeProps: options.treeProps || { value: 'id', label: 'label', children: 'children' },
  checkStrictly: options.checkStrictly,
  multiple: options.multiple,
  span: options.span,
  rules: options.rules,
  props: options.props,
  ...options,
})

/**
 * 创建自定义插槽配置
 */
export const createSlot = (label, prop, options = {}) => ({
  type: 'slot',
  label,
  prop,
  slotName: options.slotName || prop,
  span: options.span,
  rules: options.rules,
  ...options,
})

/**
 * 创建空白占位配置
 */
export const createEmpty = (options = {}) => ({
  type: 'empty',
  span: options.span || 8,
})

/**
 * 常用验证规则
 */
export const commonRules = {
  required: message => ({ required: true, message, trigger: ['blur', 'change'] }),
  email: { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] },
  phone: {
    required: true,
    pattern: /^(1[3-9]\d{9}|(0\d{2,3}-?)?\d{7,8})$/,
    message: '请输入正确的手机号码',
    trigger: ['blur', 'change'],
  },
  length: (min, max, message) => ({ min, max, message, trigger: ['blur', 'change'] }),
  pattern: (pattern, message) => ({ pattern, message, trigger: ['blur', 'change'] }),
  identityCard: {
    required: true,
    pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
    message: '请输入正确的身份证号码',
    trigger: ['blur', 'change'],
  },
  // 身份证号码(15位、18位数字，最后一位可能是字母)
  identityCard18: {
    required: true,
    pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
    message: '请输入正确的身份证号码',
    trigger: ['blur', 'change'],
  },

  // 同一天不同时间
  onlyDay: { required: true, validator: validateTime, trigger: ['blur', 'change'] },

  // 邮政编码
  postCode: {
    required: true,
    pattern: /^[1-9]\d{5}$/,
    message: '请输入正确的邮政编码',
    trigger: ['blur', 'change'],
  },
  // 统一社会信用代码
  creditCode: {
    required: true,
    pattern: /^.{18}$/,
    message: '请输入正确的统一社会信用代码',
    trigger: ['blur', 'change'],
  },
}

/**
 * 创建表单配置
 */
export const createFormConfig = (options = {}) => ({
  labelWidth: options.labelWidth || '80px',
  inline: options.inline || false,
  gutter: options.gutter || 20,
  defaultSpan: options.defaultSpan || 12,
  showButtons: options.showButtons !== false,
  buttonAlign: options.buttonAlign || 'left',
  buttons: options.buttons || [
    { text: '确定', type: 'primary', action: 'submit' },
    { text: '取消', action: 'reset' },
  ],
  formProps: options.formProps || {},
  ...options,
})
